package com.canbot.u05.activity.presidemode.newcode.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ItemChoiceView extends RelativeLayout {
    private static final String TAG = "ItemChoiceView";
    private static final int LEFT = 1;
    private static final int RIGHT = 2;
    private static final int NONE = 3;
    @BindView(R.id.ib_left)
    ImageView ibLeft;
    @BindView(R.id.item_title)
    TextView itemTitle;
    @BindView(R.id.ib_right)
    ImageView ibRight;
    private int selectorPos;
    private Drawable leftDrawable;
    private Drawable rightDrawble;
    private Drawable selectorDrawble;


    public ItemChoiceView(Context context) {
        this(context, null);
    }

    public ItemChoiceView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ItemChoiceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAttrbite(context, attrs);
    }

    private boolean isSelect;

    public boolean isSelect() {
        return isSelect;
    }

    public void select() {
        isSelect = true;
        if (selectorDrawble != null) {
            if (selectorPos == LEFT) {
                ibLeft.setBackground(selectorDrawble);
            } else if (selectorPos == RIGHT) {
                ibRight.setBackground(selectorDrawble);
            }
        }
    }

    public void cancle() {
        isSelect = false;
        if (selectorPos == LEFT) {
            ibLeft.setBackground(leftDrawable);
        } else if (selectorPos == RIGHT) {
            ibRight.setBackground(rightDrawble);
        }
    }

    private void initAttrbite(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemChoiceView);
        int centerTitle = typedArray.getResourceId(R.styleable.ItemChoiceView_centerText, -1);
        leftDrawable = typedArray.getDrawable(R.styleable.ItemChoiceView_leftDrawable);
        rightDrawble = typedArray.getDrawable(R.styleable.ItemChoiceView_rightDrawable);
        selectorDrawble = typedArray.getDrawable(R.styleable.ItemChoiceView_selectorDrawable);
        selectorPos = typedArray.getInteger(R.styleable.ItemChoiceView_selecorPositon, 1);

        RelativeLayout.LayoutParams layoutParamL = (LayoutParams) ibLeft.getLayoutParams();
        RelativeLayout.LayoutParams layoutParamR = (LayoutParams) ibRight.getLayoutParams();
        int dimension24 = (int) context.getResources().getDimension(R.dimen.px24);
        int dimension52 = (int) context.getResources().getDimension(R.dimen.px40);
        if (selectorPos == LEFT) {
            layoutParamL.setMarginStart(dimension24);
            layoutParamR.setMarginEnd(dimension52);
        } else if (selectorPos == RIGHT) {
            layoutParamL.setMarginStart(dimension52);
            layoutParamR.setMarginEnd(dimension24);
        } else {
            layoutParamL.setMarginStart(dimension52);
            layoutParamR.setMarginEnd(dimension52);
        }
        ibLeft.setLayoutParams(layoutParamL);
        ibRight.setLayoutParams(layoutParamR);

        if (rightDrawble != null) {
            ibRight.setVisibility(VISIBLE);
            ibRight.setBackground(rightDrawble);
        }
        ibLeft.setBackground(leftDrawable);

        itemTitle.setText(getContext().getString(centerTitle));
        typedArray.recycle();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_choice_view, this);
        ButterKnife.bind(this, view);
    }
}
